package pdn.sci.cs.action;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.struts2.interceptor.SessionAware;

import pdn.sci.cs.entity.BaseEntity;
import pdn.sci.cs.entity.SessionKey;
import pdn.sci.cs.entity.SystemUser;
import pdn.sci.cs.util.Pager;

import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport implements SessionAware {

	public enum OPERATION_MODE {EDIT, ADD, VIEW};

	private static final long serialVersionUID = 1L;
	protected static final String MOBILE_VIEW = "mobile";
	protected static final String WEB_VIEW = "web";
	public static final String NO_SEARCH_RESULT = "no-result";

	protected Map<SessionKey, Object> session;
	protected boolean loggedIn;
	protected String id;
	protected boolean loginStatus;
	protected String userType;

	protected Pager pager;
	protected Integer pageStart;
	protected Integer pageSize;
	protected String targetDiv;

	protected boolean edit = false;
	protected boolean delete = false;
	protected final int SEARCH_PAGE_SIZE = 50;
	protected OPERATION_MODE operationMode = OPERATION_MODE.VIEW;


	public String show() {
		return SUCCESS;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}

	public Map<SessionKey, Object> getSession() {
		return this.session;
	}

	public boolean isLoggedIn() {
		return loggedIn;
	}

	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}

	public String searchResult(List<? extends Serializable> list, String resultResponse) {
		if(list == null || list.size() == 0) {
			return NO_SEARCH_RESULT;
		} else {
			return resultResponse;
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public boolean isLoginStatus() {
		return loginStatus;
	}

	public void setLoginStatus(boolean loginStatus) {
		this.loginStatus = loginStatus;
	}

	public boolean isLogin(){
		try{
			SystemUser user = (SystemUser)session.get(SessionKey.SESSION_USER);
			if(user != null){
				return true;
			}
			return false;
		}catch (Exception e) {
			return false;
		}
	}

	public SystemUser getUser(){
		if(session.get(SessionKey.SESSION_USER) != null) {
			return (SystemUser)session.get(SessionKey.SESSION_USER);
		} else {
			throw new RuntimeException("User session not found");
		}
	}
	
	protected void addMode() {
		operationMode = OPERATION_MODE.ADD;
	}
	
	protected void editMode() {
		operationMode = OPERATION_MODE.EDIT;
	}
	
	public OPERATION_MODE getOperationMode() {
		return operationMode;
	}

	protected void setAddSettings(BaseEntity entity) {
		setInserDatetTime(entity);
	}

	protected void setUpdateSettings(BaseEntity entity) {
		setUpdateDateTime(entity);
	}

	private void setUpdateDateTime(BaseEntity entity) {
		entity.setUpdateDatetime(Calendar.getInstance());
	}

	private void setInserDatetTime(BaseEntity entity) {
		entity.setInsertDatetime(Calendar.getInstance());
	}
	

} 

